app.controller("modifyController",function($scope, $http){
    // 修改密码
    function getCookie(cookie_name){
        var allcookies = document.cookie;
        var cookie_pos = allcookies.indexOf(cookie_name);
        if (cookie_pos != -1){
            cookie_pos += cookie_name.length + 1;
            var cookie_end = allcookies.indexOf(";", cookie_pos);
            if (cookie_end == -1){cookie_end = allcookies.length;}
            var value = unescape(allcookies.substring(cookie_pos, cookie_end));}
        return value;
    }
    // console.log(getCookie("key"))
    var cookie_val = getCookie("token");
    //6到15位字母或数字
    var reg=/^[0-9a-zA-Z]{6,15}$/;
    //新密码校验
    $("#newPassword").blur(function(){
        $("#msg").css("display","none");
        var newPassword = $("#newPassword").val();
        if(newPassword==""||newPassword==null){
            $("#msg").html("请输入新密码！").css("display","block");
        }else if(!reg.test(newPassword)){
            $("#msg").html("密码格式不正确,请重新输入！").css("display","block");
        }
    });
    //重复密码校验
    $("#repeatPassword").blur(function(){
        var newPass = $("#newPassword").val();
        var repPass = $("#repeatPassword").val();
        if(newPass != repPass){
            $("#ms").html("两次输入密码不一致,请重输！").css("display","block");
        }
    });
    //表单提交前校验
    $("#updateBtn").click(function(){
        var flag = true;
        var newPass = $("#newPassword").val();
        var repPass = $("#repeatPassword").val();
        var oldPassword=$("#oldPassword").val();
        var reg1=/^[0-9a-zA-Z]{6,15}$/;
        if(!reg1.test(newPass)||newPass!=repPass||newPass ==""||newPass==null){
            flag = false;
        }
        if(flag){
            $.ajax({
                url:"/OA/staff/modifyPassword",
                type:"POST",
                async:false,
                data:{id:cookie_val,old_password:oldPassword,password:newPass}, //表单数据序列化, 可以对form表单进行序列化，从而将form表单中的所有参数传递到服务端。
                success:function(data){
                    if(data.ok==true){
                        alert("修改成功！")
                    }
                },
                error:function(e){
                    alert("请求出错！");
                }
            });
        }
    });
})